package com.allenkerr.vo;

import static com.allenkerr.vo.Result.ResultEnum.FAIL;
import static com.allenkerr.vo.Result.ResultEnum.SUCC;

/**
 * Created by 小柯 on 2017/10/16.
 */
public class Result<T> {

  private int code;

  private String msg;

  private T data;

  public Result() {
  }

  public Result(int code, String msg, T data) {
    this.code = code;
    this.msg = msg;
    this.data = data;
  }

  public static <T> Result<T> success(int code, String msg, T data) {
    return new Result(code, msg, data);
  }

  public static Result success(int code, String msg) {
    return success(code, msg, null);
  }

  public static <T> Result<T> success(String msg, T data) {
    return success(SUCC.getCode(), msg, data);
  }

  public static <T> Result<T> success(T data) {
    return success(SUCC.getText(), data);
  }

  public static Result success(String msg) {
    return success(msg, null);
  }

  public static Result success() {
    return success(SUCC.getText(), null);
  }

  public static <T> Result<T> fail(int code, String msg, T data) {
    return new Result(code, msg, data);
  }

  public static Result fail(int code, String msg) {
    return fail(code, msg, null);
  }

  public static <T> Result<T> fail(String msg, T data) {
    return fail(FAIL.getCode(), msg, data);
  }

  public static <T> Result<T> fail(T data) {
    return fail(FAIL.getText(), data);
  }

  public static Result fail(String msg) {
    return fail(FAIL.getCode(), msg);
  }

  public static Result fail() {
    return fail(null);
  }

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  enum ResultEnum {
    SUCC(0, "成功"),
    FAIL(1, "失败");

    ResultEnum(int code, String text) {
      this.code = code;
      this.text = text;
    }

    private int code;
    private String text;

    public int getCode() {
      return code;
    }

    public String getText() {
      return text;
    }
  }

}
